#include "xlsxproctask.h"

xlsxProcTask::xlsxProcTask() : QObject() {
  qRegisterMetaType<xlsxParam_t>("xlsxParam_t");

  tasks.setStackSize(16 * 1024 * 1024);
  moveToThread(&workThread);
  workThread.start();
  xlsx = NULL;
}
xlsxProcTask::~xlsxProcTask() { delete xlsx; }
void xlsxProcTask::setWorkParam(const xlsxParam_t &param) {
  xlstName = xlsxParam.xlsxName;
  xlsxParam = param;
  xlsxParam.xlsxName = param.xlsxName;
  xlsxParam.savePath = param.savePath;
  xlsxParam.sector.classType = param.sector.classType;
  xlsxParam.sector.classList = param.sector.classList;
  xlsxParam.delColumn.isDel = param.delColumn.isDel;
  xlsxParam.delColumn.delCoumnList = param.delColumn.delCoumnList;
  xlsxParam.calulation.isAdd = param.calulation.isAdd;
  xlsxParam.calulation.coumnName = param.calulation.coumnName;
  xlsxParam.calulation.AddCoumnList = param.calulation.AddCoumnList;

  if (xlsxParam.xlsxName.isEmpty())
    return;

  if (xlsx != NULL && (0 == QString::compare(xlstName, xlsxParam.xlsxName))) {
    return;
  }

  if (xlsx != NULL) {
    delete xlsx;
    xlsx = NULL;
  }
  xlsx = new Document(xlsxParam.xlsxName);
}

QString xlsxProcTask::errrorString() { return errString; }

void xlsxProcTask::removeListSame(QStringList *list) {
  for (int i = 0; i < list->count(); i++) {
    for (int k = i + 1; k < list->count(); k++) {
      if (list->at(i) == list->at(k)) {
        list->removeAt(k);
        k--;
      }
    }
  }
}

void xlsxProcTask::procxlsx(const int &row, const int &coulmn,
                            const QString &savePath) {
  int cnt6, tmpCount;
  QString saveFilePath;
  QStringList srcheadList, listCoulnm, ct6List;

  for (cnt6 = 0; cnt6 < coulmn; cnt6++) {
    listCoulnm.append(xlsx->read(1, cnt6 + 1).toString());
  }
  srcheadList = listCoulnm;

  switch (xlsxParam.sector.classType) {
  case fiveGet:
    cnt6 = listCoulnm.indexOf("五级部门");
    break;
  case sixGet:
  case sixSplit:
    cnt6 = listCoulnm.indexOf("六级部门");
    break;
  case noUse:
  default:
    return;
  }

  if (cnt6 == -1)
    return;
  /* 提取列 */
  ct6List.clear();
  for (int i = 0; i < row - 1; i++) {
    ct6List.append(xlsx->read(i + 1, cnt6 + 1).toString());
  }
  removeListSame(&ct6List);

  if (xlsxParam.sector.classType == fiveGet ||
      xlsxParam.sector.classType == sixGet) {
    for (int cnt = ct6List.count() - 1; cnt >= 0; cnt--) {
      if (-1 == xlsxParam.sector.classList.indexOf(ct6List.at(cnt))) {
        ct6List.removeAt(cnt);
      }
    }
  }

  /* ct6只有需要过滤的项目 */
  for (int cnt = 0; cnt < ct6List.count(); cnt++) {
    if (ct6List.at(cnt).isEmpty())
      continue;
    saveFilePath = savePath + "/" + ct6List.at(cnt) + ".xlsx";

    Document saveXlsx;
    /* 计算表头 */

    if (xlsxParam.delColumn.isDel) {
      for (int delCnt = 0; delCnt < xlsxParam.delColumn.delCoumnList.count();
           delCnt++) {
        tmpCount =
            listCoulnm.indexOf(xlsxParam.delColumn.delCoumnList.at(delCnt));
        if (tmpCount == -1) {
          continue;
        }
        listCoulnm.removeAt(tmpCount);
      }
    }

    for (int inCnt = 0; inCnt < listCoulnm.count(); inCnt++) {
      saveXlsx.write(1, inCnt + 1, listCoulnm.at(inCnt));
    }

    /* 写入后续数据 */
    for (int rowCnt = 2, rTmp = 0, skipCnt = 1; rowCnt <= row; rowCnt++) {
      /* 行过滤 */
      if (xlsxParam.sector.classType == sixSplit ||
          xlsxParam.sector.classType == sixGet) {
        rTmp = srcheadList.indexOf("六级部门");
      } else {
        rTmp = srcheadList.indexOf("五级部门");
      }

      if (rTmp == -1)
        continue;
      if (0 != QString::compare(ct6List.at(cnt),
                                xlsx->read(rowCnt, rTmp + 1).toString(),
                                Qt::CaseSensitive)) {
        continue;
      }
      skipCnt++;
      for (int icolumn = 0, tmpCnt = 0; icolumn < listCoulnm.count();
           icolumn++) {
        tmpCnt = srcheadList.indexOf(listCoulnm.at(icolumn)); /* 列过滤 */
        if (tmpCnt == -1)
          continue;
        saveXlsx.write(skipCnt, icolumn + 1, xlsx->read(rowCnt, tmpCnt + 1));
      }
    }
    if (1 >= saveXlsx.dimension().rowCount()) {
      continue;
    }

    saveXlsx.saveAs(saveFilePath);
  }
}

void xlsxProcTask::exportXlsx() {
  QStringList sheetList = xlsx->sheetNames();
  QXlsx::CellRange range;
  int row, coulmn;
  QString savePath;
  QFileInfo testFile;
  QDir testDir;
  QStringList ct5List, ct6List, ct6TmpList, listCoulnm, rowDataList,
      srcheadList;
  for (int sheetCount = 0; sheetCount < sheetList.count(); sheetCount++) {
    /* 选择sheet */
    xlsx->selectSheet(sheetList.at(sheetCount));

    range = xlsx->dimension();
    row = range.lastRow();
    coulmn = range.lastColumn();

    /**/
    savePath = xlsxParam.savePath + "/" + sheetList.at(sheetCount);
    testFile.setFile(savePath);
    if (testFile.isFile()) {
      testFile.dir().remove(savePath);
    }
    testDir.mkdir(savePath);
    listCoulnm.clear();
    procxlsx(row, coulmn, savePath);
  }
  emit exportFinished();
}
